package com.example.work4.view

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.example.work4.DataUtil
import com.example.work4.ImageActivity
import com.example.work4.R
import com.example.work4.common.ImageUtil
import com.example.work4.http.FriendRetrofitImp
import com.example.work4.po.Moments
import com.example.work4.po.ResultBean
import retrofit2.Call
import retrofit2.Response
import javax.security.auth.callback.Callback

class MomentDialog(var moments: Moments,var mContext: Context):BottomDialog(mContext,R.layout.dialog_mark),
View.OnClickListener{

    var avatar : ImageView?=null

    var image : ImageView?=null

    var content : TextView?=null

    var name: TextView?=null

    var location:TextView?=null

    var exit:Button?=null

    var follow:Button?=null

    override fun init()
    {
        name = findViewById(R.id.dialog_nickname)
        content = findViewById(R.id.dialog_content)
        avatar = findViewById(R.id.dialog_avatar)
        location = findViewById(R.id.dialog_location)
        follow = findViewById(R.id.dialog_follow)
        exit = findViewById(R.id.dialog_exit)
        image = findViewById(R.id.dialog_picture)

        name?.text=moments.username
        content?.text=moments.content
        location?.text = moments.address

        val mGlide=ImageUtil(mContext)
        mGlide.GlideLoadCenterCrop(moments.imageUrl, image!!)
        mGlide.GlideLoadCenterCrop(moments.avatar, avatar!!)



        exit?.setOnClickListener(this)
        image?.setOnClickListener(this)
        follow?.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when(v?.id)
        {
            R.id.dialog_follow->
            {
                val friendRetrofitImp = FriendRetrofitImp()
                friendRetrofitImp.addCall(moments.userId).enqueue(object : retrofit2.Callback<ResultBean<Map<String, String>>> {
                    override fun onResponse(call: Call<ResultBean<Map<String, String>>>,
                                            response: Response<ResultBean<Map<String, String>>>) {
                       if(response.code() == 200){
                           Toast.makeText(context, "关注成功！", Toast.LENGTH_SHORT).show()
                           follow!!.setBackgroundResource(R.drawable.vector_right)
                           DataUtil.messageHandler!!.sendEmptyMessage(DataUtil.UPDATE_FRIEND_LIST)
                       }
                    }

                    override fun onFailure(call: Call<ResultBean<Map<String, String>>>,
                                           t: Throwable) {

                    }

                })

            }

            R.id.dialog_exit ->
            {
                this.dismiss()
            }

            R.id.dialog_picture ->
            {
                var intent= Intent(mContext, ImageActivity::class.java)
                intent.putExtra("imageUri",moments.imageUrl)
                mContext.startActivity(intent)
            }
        }
    }



}